<?php
session_start();
require_once ("config.inc.php");
require_once ("xajaxCommon.php");


function deploy($mycountry,$mynumarmies) {
	$objResponse 	= new xajaxResponse();
	$r = ActionDeploy($mycountry,$mynumarmies,$_SESSION['RISK_USERNUM']);
	if($r['cant'] != NULL) {	
		$objResponse->addScript("alert('".$r['cant']."')");
	}	
	if($r['attack'] != NULL) {	
		$objResponse->addScript("DisplayBottom('attack.php');");
		$objResponse->addScript("set_turnstatus('Attacking');");	
	}
	$objResponse->addScript("document.deploy.send.disabled = false;");
	$objResponse->addScript("intervalfunctions();");
	return $objResponse;
}
 
function attack($myfrom,$myto,$mynumarmies) {	
	$objResponse 	= new xajaxResponse();	
	$r = ActionAttack($myfrom,$myto,$mynumarmies,$_SESSION['RISK_USERNUM']);	
	if($r['cant'] != NULL) 			{ 
		$objResponse->addScript("alert('".$r['cant']."')"); 
	}	
	if ($r['takeover'] == true)	{ 
		$objResponse->addScript("DisplayBottom('takeover.php?from=$myfrom&to=$myto&numarmies=$mynumarmies');");	
		$objResponse->addScript("set_turnstatus('Takeover');");	
	}
	$objResponse->addScript("document.attack.send.disabled = false;");
	$objResponse->addScript("intervalfunctions();");
	return $objResponse;		 
}

function attackTillEnd($myfrom,$myto,$mynumarmies) {
	$objResponse 	= new xajaxResponse();	
	$cdb 			= new class_mysql;
	$takeover 	= false;
	while(($takeover == false) && ($mynumarmies > 0)) {
		$r = ActionAttack($myfrom,$myto,$mynumarmies,$_SESSION['RISK_USERNUM']);
		if($r['cant'] != NULL) 			{ 
			$objResponse->addScript("alert('".$r['cant']."')"); 
			$objResponse->addScript("document.attack.send.disabled = false;");
			$objResponse->addScript("intervalfunctions();");
			return $objResponse;
		}		
		if ($r['takeover'] == true)	{ 
			$objResponse->addScript("DisplayBottom('takeover.php?from=$myfrom&to=$myto&numarmies=$mynumarmies');");	
			$objResponse->addScript("set_turnstatus('Takeover');");	
			$takeover = true;
		}
		$cdb->select("select armies from risk_countries where num = '$myfrom' ");
		$daten 			= $cdb->fetchArray();
		$mynumarmies 	= $daten['armies']-1;		
		if($mynumarmies > 3) { $mynumarmies = 3; }
	}
	$objResponse->addScript("document.attack.send.disabled = false;");
	$objResponse->addScript("intervalfunctions();");

	return $objResponse;		 
}
 
function takeover($myfrom,$myto,$mynumarmies) {
	$objResponse 	= new xajaxResponse();	
	$r = ActionTakeover($myfrom,$myto,$mynumarmies,$_SESSION['RISK_USERNUM']);
	if($r['ok'] != NULL) {	
		$objResponse->addScript($r['ok']);
		$objResponse->addScript("set_turnstatus('Attacking');");
	}
	$objResponse->addScript("intervalfunctions();");
	if($r['win'] != NULL) {	
		$objResponse->addScript($r['win']);
	}
	return $objResponse;		 
}
 
function fortify($mynumarmies,$myfrom,$myto) {
	$objResponse 	= new xajaxResponse();
	$r = ActionFortify($mynumarmies,$myfrom,$myto, $_SESSION['RISK_USERNUM']);
	$cant 	= $r['cant'];
	$ok 	= $r['ok'];	
	if ($ok == true) {								
		$objResponse->addScript("DisplayBottom('notmyturn.php');");  
		$objResponse->addScript("document.main.myturn.value = 0;");  
		$objResponse->addScript("set_turnstatus('NotMyTurn');");
		$objResponse->addScript("intervalfunctions();");	
	}	
	if ($cant != NULL) {
		$objResponse->addScript("alert(\"$cant\");");
		$objResponse->addScript("document.fortify.send.disabled = false;");  
		$objResponse->addScript("document.fortify.skip.disabled = false;");  
	}
	return $objResponse;		 
}

function trade($mycards) {
	$objResponse 	= new xajaxResponse();
	$r 		= ActionTrade($mycards,$_SESSION['RISK_USERNUM']);
	$cant 	= $r['cant'];
	$ok 	= $r['ok'];
	if ($cant != NULL) { $objResponse->addScript("alert('$cant');");  }
	if ($ok   == true) {
		$objResponse->addScript("DisplayBottom('deploy.php');"); 
		$objResponse->addScript("set_turnstatus('Deploy');");	
	}
	$objResponse->addScript("document.playerlist.trade.disabled = false;");
	return $objResponse;		
}

function quit() {
	global $risk;
	$objResponse 	= new xajaxResponse();	
	$cdb 			= new class_mysql;
	$cdb->select("SELECT CurrentGame FROM risk_config ");
	$row 			= $cdb->fetchArray();
	$CurrentGame 	= $daten['CurrentGame'];
	
	$cdb->select("SELECT turnflag FROM risk_players WHERE num='".$_SESSION['RISK_USERNUM']."'");
	$row 			= $cdb->fetchArray();
	$turnflag 		= $daten['turnflag'];
	
	$cdb->select("SELECT color, colornum FROM risk_colors WHERE assigned='".$_SESSION['RISK_USERNUM']."'");
	$row 			= $cdb->fetchArray();
	$mycolor 		= $row['color'];
	$colornum 		= $row['colornum'];
	$cdb->insert("INSERT INTO risk_dialog VALUES('1', '".date("Y-m-d H:i:s")."', 
				  '<font color=\'$mycolor\'>* ".$_SESSION['RISK_USERNAME']." has left the lobby.</font>')");
				  
	$cdb->query("UPDATE risk_players SET status='1' WHERE num='".$_SESSION['RISK_USERNUM']."'");
	$cdb->query("UPDATE risk_colors SET assigned='0' WHERE assigned='".$_SESSION['RISK_USERNUM']."'");
	if(($CurrentGame != "starting")) { 		
		$cdb->select("select count(*) from risk_players where num > 6 and turnorder >0 and num != '".$_SESSION['RISK_USERNUM']."'");
		$daten 			= $cdb->fetchArray();
		$countothers = $daten[0];
		$cdb->select("select num from risk_players where num < 7 and turnorder=0 limit 1");
		$daten 		= $cdb->fetchArray();
		$risk->assignColor($daten['num'], $colornum);
		if($countothers > 0) {
			$risk->check4botmove();
		}
	}	
	$objResponse->addScript("document.location.href='index.php';");
	unset($_SESSION['RISK_LASTREQUEST']);
	unset($_SESSION['RISK_LASTCHATREQUEST']);
	return $objResponse;		
}

function kick($who, $why) {
	global $risk;
	$objResponse 	= new xajaxResponse();
	$risk->risk_kick($who, $why,$_SESSION['RISK_USERNAME']);
	$script = "alert('Player kicked.');";
	$objResponse->addScript($script);  
	return $objResponse;		
}
	 	
function poke($who) {
	$objResponse 	= new xajaxResponse();
	$cdb   			= new class_mysql;
	$cdb->query("UPDATE risk_players SET pokeflag='".$_SESSION['RISK_USERNAME']."' WHERE num='$who'");
	$script = "alert('Player poked.');";
	$objResponse->addScript($script);  
	return $objResponse;		
}

function newTurn() {
	$objResponse 	= new xajaxResponse();
	if( ActionNewTurn($_SESSION['RISK_USERNUM']) == true) {
		$script = "DisplayBottom('deploy.php');";
		$objResponse->addScript($script);
	}
	$script = "document.main.myturn.value='1';";
	$objResponse->addScript($script);  	
	return $objResponse;		
}

function deployForm() {
	$cdb    		= new class_mysql;
	$objResponse 	= new xajaxResponse();
	$cdb->select("SELECT sum(armies) as sArmies FROM risk_players");
	$row 	= $cdb->fetchArray();
	if ($row['sArmies'] == 0) {
		$script = "DisplayBottom('attack.php');";
		$objResponse->addScript($script);  
		return $objResponse;		
	} 
	$script = '
	var r = "";
	r += "<form name=\'deploy\' onsubmit=\'return false\'>";
	r += "<table width=\'100%\'><tr>";
	r += "<td ><b>&nbsp; &nbsp; Deploy &nbsp; &nbsp; </b></td>";
	r += "<td align=\'center\'> '.armiesselecta().' </td>";
	r += "<td align=\'center\' ><b>&nbsp; &nbsp; armies &nbsp; &nbsp; in &nbsp; &nbsp; </b></td>";
	r += "<td align=\'center\'>'.countryselecta().'</td>";
	r += "<td align=\'center\' width=\'180\'>";
	r += "<input type=\'button\' name=\'send\' value=\':: Deploy armies ::\' onclick=\'DeployArmies()\'> &nbsp;";
	r += "</td></tr></table>";
	r += "</form>";
	document.getElementById(\'bottom\').innerHTML = r; ';
	$objResponse->addScript($script);  
	return $objResponse;		
}

function attackForm() {
	$cdb    		= new class_mysql;
	$objResponse 	= new xajaxResponse();
	$script = '
	var r = "";
	r += "<form name=\'attack\'>";
	r += "<table width=\'100%\'><tr><td ><b>Attack :</b></td>";
	r += "<td align=\'center\'>";
	r += "<select name=\'numarmies\'><option value=\'3\'>3</option><option value=\'2\'>2</option><option value=\'1\'>1</option></select></td>";
	r += "<td align=\'center\' ><b>armies from</b></td>";
	r += "<td align=\'center\'>'.countryselecta('from').'</td>";
	r += "<td align=\'center\' ><b>to</b></td>";
	r += "<td align=\'center\'>'.countryselecta('to','<>').'</td>";
	r += "<td align=\'center\'>";
	r += "<input type=\'button\' name=\'send\' value=\'Attack till the End\' onclick=\'AttackTillEnd()\' style=\'color:Green\'>";
	r += "<input type=\'button\' name=\'send\' value=\'Attack\' onclick=\'Attack()\'  style=\'color:Green\'>";
	r += "<input type=\'button\' name=\'done\' value=\'Done\' onclick=\'AttackDone()\' style=\'color:Red\'>";
	r += "</td></tr></table></form>";
	document.getElementById(\'bottom\').innerHTML = r; ';
	$objResponse->addScript($script);  
	return $objResponse;		 
}

function takeoverForm($myfrom,$myto,$mynumarmies) {
	$cdb    		= new class_mysql;
	$objResponse 	= new xajaxResponse();	
	$cdb->select("SELECT name, continent FROM risk_countries WHERE num='$myto'");
	$row = $cdb->fetchArray();
	$script = '
	var r = "";
	r += "<form name=\'takeover\' onsubmit=\'return false\'>";
	r += "<table width=\'100%\'><tr>";
	r += "<td align=\'justify\'><b>Conquered <font class=\'c'.$row['continent'].'\'>'.$row['name'].'</font>! &nbsp;Move &nbsp;";
	';
	$cdb->select("SELECT name, armies, continent FROM risk_countries WHERE num='$myfrom'");
	$row = $cdb->fetchArray();
	$script .= '
	r += "<select name=\'numarmies\'>";	';
	while ($mynumarmies < $row['armies']) {
		$script .= '
		r += "<option value=\''.$mynumarmies.'\'>'.$mynumarmies.'</option>"; ';
		$mynumarmies++;
	}
	$script .= '
	r += "</select>";
	r += "&nbsp; from <font class=\'c'.$row['continent'].'\'>'.$row['name'].'</font> </b></td>";
	r += "<td align=\'center\' width=\'150\'>";
	r += "<input type=\'hidden\' name=\'from\' value=\''.$myfrom.'\'>";
	r += "<input type=\'hidden\' name=\'to\' value=\''.$myto.'\'>";
	r += "<input type=\'button\' name=\'send\' value=\':: Move armies ::\' onclick=\'Takeover()\'>";
	r += "</td></tr></table></form>";
	document.getElementById(\'bottom\').innerHTML = r; 	';
   
	$objResponse->addScript($script);  	
	return $objResponse;		   
}

function fortifyForm() {
	$cdb    		= new class_mysql;
	$objResponse 	= new xajaxResponse();
	$cdb->query("UPDATE risk_config SET TurnStatus='Fortify'");
	$cdb->query("UPDATE risk_players SET bottomsrc='fortify.php' WHERE num='".$_SESSION['RISK_USERNUM']."'");
	$cdb->select("select max(armies) from risk_countries where owner = '".$_SESSION['RISK_USERNUM']."'");
	$daten = $cdb->fetchArray();
	$maxarmies = $daten[0]-1;	
	$armyselecta = "<select name='numarmies'>";
	for($i = 1; $i <= $maxarmies; $i++){
		$armyselecta .= "<option value='$i'>$i</option>";
	}
	$armyselecta .= "</select>";
	$script = '
	var r = "";
	r += "<form name=\'fortify\'>";
	r += "<table width=\'100%\'> <tr><td ><b>Fortify</b></td>";
	r += "<td align=\'center\'> '.$armyselecta.'</td>";
	r += "<td align=\'center\' ><b>armies from</b></td>";
	r += " <td align=\'center\'>'.countryselecta('from').'</td>";
	r += "<td align=\'center\' ><b>to</b></td>";
	r += " <td align=\'center\'>'.countryselecta('to').'</td>";
	r += "<td align=\'center\' width=\'100\'>";
	r += "<input type=\'button\' style=\'color:Green\' name=\'send\' value=\'Fortify\' onclick=\'Fortify()\'> ";
	r += "<input type=\'button\' style=\'color:Red\' name=\'skip\' value=\'Skip\' onclick=\'FortifySkip()\'>&nbsp;";
	r += "</td></tr></table><input type=\'hidden\' name=\'flag\' value=\'0\'></form>";
	document.getElementById(\'bottom\').innerHTML = r;  ';
	$objResponse->addScript($script);  
	return $objResponse;		 
}

function countryselecta($name='country',$bedingung = "="){
	$cdb = new class_mysql;
	$r = "<select name='$name'>";
	$cdb->select("SELECT num, name, continent FROM risk_countries WHERE owner $bedingung '".$_SESSION['RISK_USERNUM']."'");
	while ($row = $cdb->fetchArray()) {
		$r .= "<option class='c".$row['continent']."' value='".$row['num']."'>".$row['name']."</option>";
	}
	$r .= "</select>";
	return $r;
}

function armiesselecta(){
	$cdb = new class_mysql;
	$usernum = $_SESSION['RISK_USERNUM'];
	$cdb->select("select armies from risk_players where num = '$usernum' ");
	$daten = $cdb->fetchArray();
	$armies = $daten[0];
	$r = "<select name='numarmies'>";
	for($i = 1; $i <= $armies; $i++){
		$r .= "<option value='$i'>$i</option>";
	}
	$r .= "</select>";
	return $r;
}
 function addDialog($msgvars) {
    $msgvars   = explode("|",$msgvars);
    $msg       = $msgvars[0];
    $to        = $msgvars[1];
    $me        = $_SESSION['RISK_USERNAME'];
    $menum     = $_SESSION['RISK_USERNUM'];
    $cdb = new class_mysql;
	$objResponse = new xajaxResponse();
    if ($msg != $null) {
     if ($to != 1) {
       $cdb->select("SELECT username, risk_colors.color FROM risk_colors LEFT JOIN risk_players ON
                     risk_colors.assigned = risk_players.num WHERE assigned='$to'");
       $row     = $cdb->fetchArray();
       $message = "<font color=\'".$row['color']."\'><b>[=>".$row['username']."]</b></font> ";
       $cdb->select("SELECT color FROM risk_colors WHERE assigned='".$me."'");
       $row     = $cdb->fetchArray();
       if (substr($msg, 0, 4) == "/me ") {
         $message .= "<font color=\'".$row['color']."\'>* ".$me. substr($msg, 3) . "</font>";
       } else {
         $message .= "<font color=\'".$row['color']."\'>".$me." : </font>". $msg;
       }
       $cdb->insert("INSERT INTO risk_dialog VALUES('".$menum."', '".date("Y-m-d H:i:s")."', '$message')");
     }

     $cdb->select("SELECT color FROM risk_colors WHERE assigned='".$me."'");
     $row    = $cdb->fetchArray();
     if (substr($msg, 0, 4) == "/me ") {
       $message = "<font color=\'".$row['color']."\'>* ".$me. substr($msg, 3) . "</font>";
     } else {
       $message = "<font color=\'".$row['color']."\'>".$me." : </font>". $msg;
     }
     $cdb->insert("INSERT INTO risk_dialog VALUES('$to', '".date("Y-m-d H:i:s")."', '$message')");
    }
	return $objResponse;
 }

function check4update($xmsg="chat",$t,$country_arr) {
	global $risk;
	$objResponse 	= new xajaxResponse();
	$cdb 			= new class_mysql;
	$update_log		= false;
	$update_chat	= false;	
	$complete 	 	= false;
	$dead 		 	= false;
	$kickedby 	 	= false;
	$pokedby 		= false;
	$myturn 		= false;
	$close 			= false;
	$start 			= false;
	$menum 			= $_SESSION['RISK_USERNUM'];
	
	// check for timeout
	$grenze = time() - 180;
	$cdb->select("SELECT num from risk_players where stamp < '$grenze' and turnflag = '1' and num > 6");
	if($cdb->getRecords() > 0) {
		$daten = $cdb->fetchArray();
		$risk->risk_kick($daten['num'], "timeout","Server");
		$script = "alert('Player kicked.');";
		$objResponse->addScript($script);  		
	}
	
	
	// UPDATE TIMESTAMP AND CHECK FOR START AND CLOSE
    $cdb->update("UPDATE risk_players SET stamp='".time()."' WHERE num='$menum'");
	$cdb->select("SELECT CurrentGame, TurnStatus FROM risk_config");
	$row         	= $cdb->fetchArray();
	$CurrentGame 	= $row['CurrentGame'];
	$turnstatus 	= $row['TurnStatus'];
	if ($CurrentGame == "Inactive") {
		$close = $turnstatus;
	} elseif ($CurrentGame == "Deploying" || $CurrentGame == "Active") {
		$cdb->select("SELECT rejoinflag FROM risk_players WHERE num='$menum'");
		$row  = $cdb->fetchArray();
		if ($row['rejoinflag'] == 1) {
			$cdb->update("UPDATE risk_players SET rejoinflag='0' WHERE num='$menum'");
			$start = "1";
		} else {
			if ($CurrentGame == "Deploying")  {
				$cdb->select("SELECT status FROM risk_players WHERE num='$menum'");
				$row = $cdb->fetchArray();
				if($row['status'] == 6)  {
					$cdb->update("UPDATE risk_players SET status='10' WHERE num='$menum'");
					$start = "1";
				}
			}
		}
		if($start == '1') {
			$objResponse->addScript("startGame();"); 
			return $objResponse;
		}
	}
	
	// CHECK FOR CHAT UPDATE !
	if(isset($_SESSION['RISK_LASTCHATREQUEST'])) {
		$where 		= "where num > '".$_SESSION['RISK_LASTCHATREQUEST']."'";
		$cdb->select("select stamp from risk_dialog $where");
		$count_chat = $cdb->getRecords();
		if($count_chat > 0) { $update_chat = true; }
	} else { $update_chat 	= true;	}
	
	// CHECK FOR GAME UPDATE !
	if(isset($_SESSION['RISK_LASTREQUEST'])) {
		$where 		= "where num > '".$_SESSION['RISK_LASTREQUEST']."'";
		$cdb->select("select stamp from risk_log $where");
		$count_log 	= $cdb->getRecords();
		if($count_log  > 0) { $update_log  = true; }
	} else { $update_log = true; }
	
	// UPDATE CHAT
	if($update_chat == true) { 
		if($xmsg == "chat") {							
			if(isset($_SESSION['RISK_LASTCHATREQUEST'])) {
				$where = "where num > '".$_SESSION['RISK_LASTCHATREQUEST']."'  ORDER BY num ASC";
			} else {
				$script 	= "emptyDialog();";
				$objResponse->addScript($script);  
				$where = "  ORDER BY num ASC LIMIT 20 ";
			}		
			$cdb->select("SELECT num, stamp, log FROM risk_dialog  $where");
			while($daten = $cdb->fetchArray()) {
				$_SESSION['RISK_LASTCHATREQUEST'] = $daten['num'];
				if ($daten['num'] == $_SESSION['RISK_USERNUM']) {
					$stamp 	= $daten['stamp'];
					if (strpos($daten['log'], "><b>[=>") != false) {
						$dialog = $daten['log'];
					} else {
						$dialog = "[Private] ".$daten['log'];
					}
				} elseif ($daten['num'] == "1") {
					$stamp 	= $daten['stamp'];
					$dialog = $daten['log'];
				}
				$dialog = strip_tags($dialog);
				$dialog = utf8_decode($dialog);
				$script = "insertDialog('$stamp','$dialog');";
				$objResponse->addScript($script);
			}				
			// CHAT
			if ($t == 2) { $objResponse->addScript("setDialog();"); }
		}
		$p = $risk->game_getPlayerAndCards($menum);
		for($i=0;$i<count($p);$i++) {
			$colornum = $p[$i][0];
			$username = $p[$i][1];
			$num	  = $p[$i][2];
			$script = "setPlayer('$colornum','$username','$num');";
			$objResponse->addScript($script);
		}
	}
	
	// UPDATE GAME
	if($update_log  == true) { 
		// UPDATE LOG
		if(isset($_SESSION['RISK_LASTREQUEST'])) {
			$where = "where num > '".$_SESSION['RISK_LASTREQUEST']."'  ORDER BY num ASC";
		} else {
			$script = "emptyLog();";
			$objResponse->addScript($script);  
			$where 	= "  ORDER BY num DESC LIMIT 20 ";
		}     
		$cdb->select("SELECT * FROM risk_log $where");
		while($row = $cdb->fetchArray()){
			$_SESSION['RISK_LASTREQUEST'] = $row['num'];
			$stamp  = $row['stamp'];
			$dialog = $row['log'];
			$dialog = strip_tags($dialog);
			$dialog = utf8_decode($dialog);
			$dialog = filter($dialog);
			$script = "insertLog('$stamp','$dialog');";
			$objResponse->addScript($script);
		}
		// UPDATE COUNTRIES		
		if($CurrentGame != "Starting") {
			$cdb->select("SELECT c.name,c.armies,p.color FROM risk_countries c, risk_players p 
						  where p.num = c.owner");
			while($daten = $cdb->fetchArray()) {
				$name   = $daten[0];
				$name 	= str_replace(" ", "_", $name);
				$armies = $daten[1];
				$color  = $daten[2];		
				$c 		= $risk-> getColor($color,'rgb');
				$r 		= $c[0];
				$g 		= $c[1];
				$b 		= $c[2];			
				$check 	= $armies.'|'.$r.'|'.$g.'|'.$b;
				if(!isset($country_arr[$name]) || ($country_arr[$name] != $check)) {
					$script = "setCountry('$name','$armies',$r,$g,$b)";			
					$objResponse->addScript($script);
				}
			}
		}
		// UPDATE PLAYER AND CARDS
		$p = $risk->game_getPlayerAndCards($menum);
		for($i=0;$i<count($p);$i++) {
			$colornum = $p[$i][0];
			$username = $p[$i][1];
			$num	  = $p[$i][2];
			$mine	  = $p[$i][3];
			$cards 	  = $p[$i][4];
			$r 		  = array();
			for($j=0;$j<count($cards);$j++) {
				$cnum  = $cards[$j][0];
				$cname = $cards[$j][1];
				$csym  = $cards[$j][2];
				$r[] = "new Array('$cnum','$cname','$csym')";
			}		
			$jsarr 	= "new Array()";
			if(count($r) > 0) { $jsarr = "new Array(".implode(',',$r).")"; } 
			$script = "setPlayerAndCards('$colornum','$username','$num','$mine',$jsarr);";
			$objResponse->addScript($script);
		}
			
		// UPDATE GAME VARS			
		if($CurrentGame != "Starting") {	
			// GET CURRENT TURN
			$cdb->select("SELECT num, username, armies, color FROM risk_players WHERE turnflag='1'");
			$row    	 = $cdb->fetchArray();
			$cuser 		 = $row['username'];			
			// SET TURN COLORS & CHECK FOR NEW TURN
			if ($row['num'] == $menum) {
				$fgcolor 	= 'black';
				$bgcolor 	= $row['color'];
				if ($CurrentGame == "Active") {
				$myturn = true;	
				}
			} else {
				$fgcolor 	= $row['color'];
				$bgcolor 	= 'black';
			}		
			$script = "setTurnvars('$turnstatus','$fgcolor','$bgcolor','$cuser');";
			$objResponse->addScript($script);
			
			// CHECK FOR VICTORY
			if ($CurrentGame == "Complete"){
				$complete = "1";
			} else {
				// CHECK FOR POKE, KICK OR DEATH
				$cdb->select("SELECT kickflag, status, pokeflag, armies FROM risk_players WHERE num='$menum'");
				$row    	 = $cdb->fetchArray();
				if 	   ($row['kickflag'] != NULL) {	$kickedby 	= $row['kickflag']; } 
				elseif ($row['status'] == 1) 	  {	$dead 		= "1";				} 
				elseif ($row['pokeflag'] != NULL) {	$pokedby 	= $row['pokeflag'];
													$cdb->update("UPDATE risk_players SET pokeflag=NULL WHERE num='$usernum'");
												  }
			}
		}			
		if($xmsg == "chat") {		
			// UPDATE ARMIES
			$cdb->select("SELECT color, armies FROM risk_players WHERE num='".$_SESSION['RISK_USERNUM']."'");
			$row 	= $cdb->fetchArray();
			$script = "insertArmies(".$row["armies"].",'".$row["color"]."');";
			$objResponse->addScript($script);
			
			// ARMIES
			if ($t == 1) 	{ $script = "setArmies();"; $objResponse->addScript($script);	}	
			if($dead) 	  	{ $script = "dead();";	 		$objResponse->addScript($script);	}
			if($myturn)  	{ $script = "myturn();";  		$objResponse->addScript($script);	}
			if($kickedby) 	{ $script = "kickedBy('$kickedby');"; 		$objResponse->addScript($script);	}
			if($pokedby)  	{ $script = "pokedBy('$pokedby');";  		$objResponse->addScript($script);	}
		}				
		if($close) 		{ $objResponse->addScript("endGame('$close');"); }
		if($complete) 	{ $script = "completeGame();";	$objResponse->addScript($script);	}
		if ($t == 3) 	{ $script = "setLog();";		$objResponse->addScript($script);	}			
	}	
	return $objResponse;
}

$xajax->processRequests();

?>